
import mechanicalsoup
import re


def search_bing_videos(keyword):
    browser = mechanicalsoup.StatefulBrowser()
    url = f"https://cn.bing.com/videos/search?q={keyword}&FORM=HDRSC3"
    browser.open(url)
    page = browser.get_current_page()
    elements = page.find_all(class_='mc_vtvc_con_rc')
    results = []
    for element in elements:
        title_match = re.search(r'"vt":"([^"]+)"', str(element))
        url_match = re.search(r'"purl":"([^"]+)"', str(element))
        if title_match and url_match:
            title = title_match.group(1)
            url = url_match.group(1)
            results.append((title, url))
    return results


if __name__ == '__main__':
    keyword = "B站python教程"
    videos = search_bing_videos(keyword)
    for title, url in videos:
        print(title+"\n", url)
        print("*******")




